<template>
<h2>添加酒店</h2>

       <table class="table table-bordered">

            <tr>
                <th>酒店名称<span style="color: red;">*</span></th>
                <th>
                    <input type="text" v-model="TianJIa1.jname">
                </th>
            </tr>
            <tr>
                <th>酒店地址 <span style="color: red;">*</span></th>
                <th>
                   <select  v-model="TianJIa1.did">
                    <option value="-1"> --请选择---</option>
                    <option v-for="item in aa" :value="item.did"> {{ item.dname }}</option>
                   </select>
                    
                </th>
            </tr>



            <tr>
                <th>酒店图片<span style="color: red;">*</span></th>
                <th>
                    <img  :src=" TianJIa1.jimg" style="height: 100px; width: 100px;">
                    <input type="file" @change="UpFile">
                    
                </th>
            </tr>


            <tr>
                <th>酒店手机号 <span style="color: red;">*</span></th>
                <th>
                    <input type="text" v-model="TianJIa1.phone">
                </th>
            </tr>


            <tr>
                <th>酒店状态 <span style="color: red;">*</span></th>
                <th>
                    <input type="radio" :value="true" v-model="TianJIa1.jsatic">开业
                    <input type="radio" :value="false" v-model="TianJIa1.jsatic">停业
                </th>
            </tr>


            <tr>
                <th>创建时间 <span style="color: red;">*</span></th>
                <th>
                    <input type="date" v-model="TianJIa1.stime" >
                </th>
            </tr>


            <tr>
                <th>交通 <span style="color: red;">*</span></th>
                <th>
                    <input type="text" v-model="TianJIa1.jiaoTong ">
                </th>
            </tr>


            <tr>
                <th>政策 <span style="color: red;">*</span></th>
                <th>
                    <input type="text" v-model="TianJIa1.zhengce" >
                </th>
            </tr>


            <tr>
                <th></th>
                <th>
                   <button class="btn btn-success" @click="AddJiu">添加</button>
                </th>
            </tr>


         


       </table> 

</template>


<script setup lang="ts">
import { onMounted } from 'vue';   


import { ref } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import router from '@/router';



import  {useHotelStore} from '../Store/Hotell'

 const  hotelstore=useHotelStore();


onMounted(()=>{   //钩子函数
    XiaLa();

})


const aa = ref([{   //响应式数据

    "did": -1,
    "dname": "string"

}])
//下拉框显示
 const XiaLa=()=>{   


        axios.get('https://localhost:7240/api/User/ShowDi').then(res=>{

                aa.value=res.data

        })

 }




const UpFile=(e:any)=>{   //上传图片


    var a =e.target.files[0];

    var b = new FormData();
    
    b.append("image",a)

    axios.post('https://localhost:7240/api/Upfile/Upfile',b).then(res=>{


        console.log(res.data);
                if(res.data.id==-1)
                {
                    alert(res.data.tiShi)
                }
               else if(res.data.id==-2)
                {
                    alert(res.data.tiShi)

                }
                else
                {
                    TianJIa1.value.jimg=res.data.path;
       
                    alert(res.data.tiShi)

                }
                  

    })
    




}






const TianJIa1=ref({     //响应式数据

"jid": 0,
  "jname": "",
  "did": -1,
  "jimg": "",
  "phone": "",
  "jsatic": "",
  "stime": "",
  "jiaoTong": "",
  "zhengce": "",
  "isDel": true


})




        const  AddJiu=()=>{   //酒店添加



        if(TianJIa1.value.jname=="")
        {
            alert('姓名不能为空')
                return ;
        }

        if(TianJIa1.value.did==-1)
        {
            alert('请选择地区')
                return ;
        }


            if(TianJIa1.value.jimg=="")
        {
            alert('请上传图片')
        }

        if(TianJIa1.value.phone=="")
        {
            alert('手机号不能为空')
                return ;
        }

        if(TianJIa1.value.jsatic=="")
        {
            alert('请选择状态不能为空')
                return ;
        }

    

        if(TianJIa1.value.stime=="")
        {
            alert('请选择开业时间')
                return ;
        }


        if(TianJIa1.value.jiaoTong=="")
        {
            alert('交通不能为空')
                return ;
        }

        
        if(TianJIa1.value.zhengce=="")
        {
            alert('政策不能为空')
                return ;
        }

        
     
            
                axios.post('https://localhost:7240/api/User/AddJiuDian',TianJIa1.value).then(res=>{


                        if(res.data>0)
                        {
                            alert("添加成功")
                            hotelstore.Mid = ""
                            router.push("/")
                        }

                        else if(res.data==-1)
                        {
                            alert('手机号重复')
                        }
                        else
                        {
                            alert('添加失败')
                        }

                })


        }






</script>